package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.datas.SearchAlbum;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by quyan on 15/10/29.
 * 专辑适配器
 */
public class SecrchAlbumAdapter extends BaseAdapter {
    private List<SearchAlbum.DataEntity> datas;
    private Context context;

    public SecrchAlbumAdapter(Context context,List<SearchAlbum.DataEntity> dataEntities) {
        this.context = context;
        this.datas = dataEntities;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.searching_album_content,null);
            holder = new ViewHolder();
            holder.albumImg = (ImageView) convertView.findViewById(R.id.album_img);
            holder.albumName = (TextView) convertView.findViewById(R.id.album_name);
            holder.albumSingerName = (TextView) convertView.findViewById(R.id.album_singer_name);
            holder.pushingTime = (TextView) convertView.findViewById(R.id.pushing_time);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //获取数据
        SearchAlbum.DataEntity dataEntity = (SearchAlbum.DataEntity) getItem(position);
        holder.albumName.setText(dataEntity.getName());//专辑名称
        holder.albumSingerName.setText(dataEntity.getSingerName());//歌手名
        holder.pushingTime.setText(dataEntity.getPublishDate());
        Picasso.with(context).load(dataEntity.getPicUrl()).into(holder.albumImg);//图片
        return convertView;
    }
    public class ViewHolder{
        private ImageView albumImg;
        private TextView albumName;
        private TextView albumSingerName;
        private TextView pushingTime;
    }
}
